package com.adv.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.adv.mapper.AdvertimgMapper;
import com.adv.mapper.AdvertposMapper;
import com.adv.model.Advertimg;
import com.adv.model.AdvertimgSqlParam;
import com.adv.model.Advertpos;
import com.adv.model.AdvertposSqlParam;
import com.adv.util.Constant;
import com.adv.util.DozerUtil;
import com.adv.vo.ProductForm;

@Component
public class AdvertposService {

	@Autowired
	AdvertposMapper advertposMapper;

	@Autowired
	AdvertimgMapper advertimgMapper;

	public List<ProductForm> getProductList() {
		AdvertposSqlParam param = new AdvertposSqlParam();
		param.createCriteria().andTypeEqualTo(Constant.AdverposType.PRODUCT.getCode());
		List<Advertpos> list = advertposMapper.selectByParam(param);
		List<ProductForm> formList = DozerUtil.mapList(list, ProductForm.class);
		if (!CollectionUtils.isEmpty(formList)) {
			for (int i = 0; i < formList.size(); i++) {
				formList.get(i).setAdvertisername(list.get(i).getAdvertiser().getName()); // with
																							// advertisername
				List<Advertimg> imgs = list.get(i).getAdvertimgList();
				if (!CollectionUtils.isEmpty(imgs)) {
					for (int j = 0; j < imgs.size(); j++) {
						Advertimg img = imgs.get(j);
						if (img.getImgtype() == 1 && img.getBusinesstype() == 1) {
							formList.get(i).setIcon(img.getIcon());
						} else if (img.getImgtype() == 2 && img.getBusinesstype() == 1) {
							formList.get(i).setBigimg(img.getIcon());
						}
					}
				}
			}
		}
		return formList;
	}

	public int changeStates(Advertpos entity) {
		return advertposMapper.updateByPrimaryKeySelective(entity);
	}

	public Advertpos getProductDetail(Advertpos product) {
		return advertposMapper.selectByPrimaryKey(product.getId());
	}

	/**
	 * save product liuyunlong at 2016��8��25��
	 * 
	 * @return
	 */
	public Map<String, Object> saveProduct(ProductForm entity) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != entity) {
			if (null == entity.getRegistertime() || "".equals(entity.getRegistertime())) {
				entity.setRegistertime(Constant.SDFCOMMON.format(new Date()));
			}
			Advertpos record = DozerUtil.mapObj(entity, Advertpos.class);
			if (null != record.getId()) {// update
				int num = advertposMapper.updateByPrimaryKeySelective(record);
				saveImg(entity, record.getId());
				if (num == 1) {
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			} else { // insert
				record.setType((short) 2); // ��Ʒ����
				advertposMapper.insertSelective(record);
				int productid = record.getId(); // 必须实体类get方法获取
				saveImg(entity, productid);
				// images
				if (productid > 0) {
					resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
				}
			}
		}
		return resultMap;
	}

	/**
	 * save icon and bigimg liuyunlong at 2016��8��26��
	 * 
	 * @return
	 */
	private void saveImg(ProductForm entity, Integer linkid) {
		if (null == entity || null == linkid) {
			return;
		}
		AdvertimgSqlParam param = new AdvertimgSqlParam();
		param.createCriteria().andLinkidEqualTo(linkid).andBusinesstypeEqualTo(Constant.BusinessType.ADVERT.getCode());
		List<Advertimg> imgs = advertimgMapper.selectByParam(param);

		if (CollectionUtils.isEmpty(imgs)) { // insert
			// icon
			Advertimg icon = new Advertimg();
			icon.setLinkid(linkid);
			icon.setBusinesstype(Constant.BusinessType.ADVERT.getCode());
			icon.setImgtype(Constant.ImageType.ICON.getCode());
			icon.setPriority(entity.getPriority());
			icon.setRegistertime(Constant.SDFCOMMON.format(new Date()));
			icon.setIcon(entity.getIcon());
			advertimgMapper.insertSelective(icon);

			// bigimg
			Advertimg bigimg = new Advertimg();
			bigimg.setLinkid(linkid);
			bigimg.setBusinesstype(Constant.BusinessType.ADVERT.getCode());
			bigimg.setImgtype(Constant.ImageType.BIGIMAGE.getCode());
			bigimg.setPriority(entity.getPriority());
			bigimg.setRegistertime(Constant.SDFCOMMON.format(new Date()));
			bigimg.setIcon(entity.getBigimg());
			advertimgMapper.insertSelective(bigimg);
		} else { // update by id
			for (int i = 0; i < imgs.size(); i++) {
				Advertimg imgu = new Advertimg();
				BeanUtils.copyProperties(imgs.get(i), imgu);
				if (imgu.getImgtype() == Constant.ImageType.ICON.getCode()) {
					imgu.setIcon(entity.getIcon());
				} else if (imgu.getImgtype() == Constant.ImageType.BIGIMAGE.getCode()) {
					imgu.setIcon(entity.getBigimg());
				}
				imgu.setRegistertime(Constant.SDFCOMMON.format(new Date()));
				imgu.setPriority(entity.getPriority());
				advertimgMapper.updateByPrimaryKey(imgu);
			}
		}
	}

	/**
	 * get product with images liuyunlong at 2016年8月26日
	 * 
	 * @return
	 */
	public ProductForm getProductByPrimaryId(Integer id) {
		Advertpos advertpos = advertposMapper.selectByPrimaryKey(id);
		ProductForm product = new ProductForm();
		BeanUtils.copyProperties(advertpos, product);
		List<Advertimg> imgs = advertpos.getAdvertimgList();
		if (!CollectionUtils.isEmpty(imgs)) {
			for (int i = 0; i < imgs.size(); i++) {
				if (imgs.get(i).getBusinesstype() == Constant.BusinessType.ADVERT.getCode()
						&& imgs.get(i).getImgtype() == Constant.ImageType.ICON.getCode()) {
					product.setIcon(imgs.get(i).getIcon());
				}
				if (imgs.get(i).getBusinesstype() == Constant.BusinessType.ADVERT.getCode()
						&& imgs.get(i).getImgtype() == Constant.ImageType.BIGIMAGE.getCode()) {
					product.setBigimg(imgs.get(i).getIcon());
				}
			}
		}
		return product;
	}

	/**
	 * get all advertisers liuyunlong at 2016年8月26日
	 * 
	 * @return
	 */
	public List<Advertpos> getAdvertiser() {
		AdvertposSqlParam param = new AdvertposSqlParam();
		param.createCriteria().andParentidEqualTo(0).andEnableEqualTo(true)
				.andTypeEqualTo(Constant.AdverposType.ADVERTISER.getCode());
		return advertposMapper.selectByParam(param);
	}

	public long getCountBySelective(AdvertposSqlParam param) {
		return advertposMapper.countByParam(param);
	}

	/**
	 * delete product and images liuyunlong at 2016年8月26日
	 * 
	 * @return
	 */
	public Object deleteProduct(ProductForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put(Constant.RESULTKEY, Constant.FAILURE);
		if (null != form && null != form.getId()) {
			// delete product
			int num = advertposMapper.deleteByPrimaryKey(form.getId());

			// delete images
			AdvertimgSqlParam param = new AdvertimgSqlParam();
			param.createCriteria().andBusinesstypeEqualTo(Constant.BusinessType.ADVERT.getCode())
					.andLinkidEqualTo(form.getId());
			int imgnums = advertimgMapper.deleteByParam(param);
			if (num == 1 && imgnums > 0) {
				resultMap.put(Constant.RESULTKEY, Constant.SUCCESS);
			}
		}
		return resultMap;
	}
}
